<template>
  <div style="height: 100%;" @click="show=false">
    <kw-topbar @updatePsd="updatePsd" @showInfo="showInfo" :show="show" @updateInfo="updateInfo" :name="name"></kw-topbar>
    <div class="main-cont">
      <kw-sidebar></kw-sidebar>
      <div class="main-wrapper">
        <transition name="fade-transform" mode="out-in">
          <router-view :key="key"/>
        </transition>
      </div>
    </div>
    <el-dialog
      title="密码修改"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="请输入旧密码" prop="oldPassword">
          <el-input type="password" v-model="form.oldPassword" ></el-input>
        </el-form-item>
        <el-form-item label="请输入新密码" prop="newPassword">
          <el-input type="password" v-model="form.newPassword"></el-input>
        </el-form-item>
        <el-form-item label="请重复新密码" prop="againPassword">
          <el-input type="password" v-model="form.againPassword"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateInfoPsd">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="用户信息修改"
      :visible.sync="dialogInfo"
      width="30%"
      >
      <el-form :model="info"  label-width="80px" class="c-form">
        <el-form-item label="用户账号">
          <el-input v-model="loginName" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="info.userName" ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input maxlength="11" v-model="info.phoneNumber" ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="info.email" ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkInfo">确 定</el-button>
        <el-button @click="dialogInfo = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import kwTopbar from '@/components/Topbar.vue'
import kwSidebar from '@/components/Sidebar.vue'
export default {
  name: 'layout',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      dialogInfo: false,
      form: {
        oldPassword: '',
        newPassword: '',
        againPassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        againPassword: [
          { trigger: 'blur', validator: validatePass2 }
        ]
      },
      info: {
        userName: '',
        phoneNumber: '',
        email: ''
      },
      loginName: '',
      show: false,
      name: ''
    }
  },
  components: {
    kwTopbar,
    kwSidebar
  },
  methods: {
    updatePsd () {
      this.dialogVisible = true
    },
    updateInfoPsd () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$http.updateInfoPassword({
            newPassword: this.form.newPassword,
            oldPassword: this.form.oldPassword
          }).then(res => {
            console.log('res', res)
            if (res.code === 20000) {
              this.$message.success('密码修改成功！')
              this.dialogVisible = false
              this.$router.push({ name: 'login' })
            } else {
              this.$message.error(res.msg)
              this.dialogVisible = false
            }
          })
        } else {
          return false
        }
      })
    },
    showInfo () {
      this.show = true
    },
    updateInfo () {
      this.dialogInfo = true
    },
    checkInfo () {
      this.$http.updateInfo(this.info).then(res => {
        if (res.code === 20000) {
          this.$message.success('信息修改成功！')
          this.dialogInfo = false
        } else {
          this.$message.error('信息修改失败！')
          this.dialogInfo = false
        }
      })
    }
  },
  computed: {
    key () {
      return this.$route.name !== undefined ? this.$route.name + new Date() : this.$route + new Date()
    }
  },
  mounted () {
    this.$http.getUserInfo('').then(res => {
      if (!res.data) {
        this.$router.push({ name: 'login' })
      } else {
        this.info.userName = res.data.userName
        this.name = res.data.userName
        this.info.phoneNumber = res.data.phonenumber
        this.info.email = res.data.email
        this.loginName = res.data.loginName
      }
    })
  }
}
</script>
